package com.schooleducation.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.schooleducation.entity.LessonOneType;
import com.schooleducation.entity.LessonTwoType;
import com.schooleducation.entity.User;
import com.schooleducation.service.ServiceFactory;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller
@RequestMapping("/admin/lessonType")
public class LessonTypeController {

	@Resource(name = "serviceFac")
	private ServiceFactory serviceFac;

	/**
	 * 获取课程类型管理界面
	 * 
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String getPage(HttpSession session, Model model) {
		User user = (User) session.getAttribute("admin");
		// 验证是否登录
		if (user != null) {
			// 获取一级分类列表
			List<LessonOneType> lots = serviceFac.getLessonService().getLessonOneTypeList();
			// 获取二级分类列表
			List<LessonTwoType> ltts = serviceFac.getLessonService().getLessonTwoTypeListByOneType(lots.get(0).getId());

			model.addAttribute("admin", user);
			model.addAttribute("lessonOneType", lots);
			model.addAttribute("lessonTwoType", ltts);

			return "admin/lessonType";
		} else {
			return "redirect:../login";
		}
	}

	/**
	 * 按一级课程类型获取二级分类请求
	 * 
	 * @param typeId
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getTwoType(int typeId) {
		List<LessonTwoType> ltts = serviceFac.getLessonService().getLessonTwoTypeListByOneType(typeId);
		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[] { "oneType" });

		return JSONArray.fromObject(ltts, config).toString();
	}

	/**
	 * 更新课程分类信息请求
	 * @param session
	 * @param data
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "updateType", method = RequestMethod.POST)
	@ResponseBody
	public String updateType(HttpSession session, String data, String type) {
		Map<String, Object> state = new HashMap<String, Object>();
		User admin = (User) session.getAttribute("admin");
		//验证是否登录
		if (admin != null) {
			state.put("isLogin", "yes");
			//选择更新类型
			if (type.equals("oneType")) {
				LessonOneType lot = (LessonOneType) JSONObject.toBean(JSONObject.fromObject(data), LessonOneType.class);
				//更新一级课程类
				if (serviceFac.getLessonService().updateLessonOneType(lot)) {
					state.put("update", "success");
				} else {
					state.put("update", "fail");
				}
			} else if (type.equals("twoType")) {
				LessonTwoType ltt = (LessonTwoType) JSONObject.toBean(JSONObject.fromObject(data), LessonTwoType.class);
				//更新二级课程类
				if (serviceFac.getLessonService().updateLessonTwoType(ltt)) {
					state.put("update", "success");
				} else {
					state.put("update", "fail");
				}
			}
		} else {
			state.put("isLogin", "no");
		}
		return JSONObject.fromObject(state).toString();
	}
	
	/**
	 * 删除课程类型数据提交
	 * @param session
	 * @param typeId
	 * 		课程类型Id
	 * @param type
	 * 		删除选择 oneType	twoType
	 * @return
	 */
	@RequestMapping(value = "deleteType", method = RequestMethod.POST)
	@ResponseBody
	public String deleteType(HttpSession session, int typeId, String type) {
		Map<String, Object> state = new HashMap<String, Object>();
		User admin = (User) session.getAttribute("admin");
		//验证是否登录
		if(admin != null) {
			state.put("isLogin", "yes");
			//选择删除类型
			if(type.equals("oneType")) {
				//删除一级课程类
				if(serviceFac.getLessonService().deleteLessonOneType(typeId)) {
					state.put("del", "success");
				} else {
					state.put("del", "fail");
				}
			} else if(type.equals("twoType")) {
				//删除二级课程类
				if(serviceFac.getLessonService().deleteLessonTwoType(typeId)) {
					state.put("del", "success");
				} else {
					state.put("del", "fail");
				}
			}
		} else {
			state.put("isLogin", "no");
		}
		return JSONObject.fromObject(state).toString();
	}
}
